package com.example.zqh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.zqh.model.RespBean;
import com.example.zqh.pojo.UserPermissionConnect;
import com.example.zqh.mapper.UserPermissionConnectMapper;
import com.example.zqh.service.UserPermissionConnectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.zqh.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 邓龙飞
 * @since 2022-09-10
 */
@Service
public class UserPermissionConnectServiceImpl extends ServiceImpl<UserPermissionConnectMapper, UserPermissionConnect> implements UserPermissionConnectService {
    @Autowired
    UserPermissionConnectMapper userPermissionConnectMapper;
    
    
    /**
      * @Author: 邓龙飞
      * @Date: 
      * @Description: 回收boss权限
    */
    @Override
    public RespBean loseBoss(Integer id) {
        UserPermissionConnect one = userPermissionConnectMapper.selectOne(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 3));
        AssertUtil.isTrue(one == null,"该用户本身没有Boss权限");
        int delete = userPermissionConnectMapper.delete(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 3));
        AssertUtil.isTrue(delete != 1,"回收boss权限失败");
        return RespBean.success("回收boss权限成功");
    }

    /**
      * @Author: 邓龙飞
      * @Date:
      * @Description: 添加boss权限
    */
    @Override
    public RespBean addBoss(Integer id) {
        UserPermissionConnect one = userPermissionConnectMapper.selectOne(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 3));
        AssertUtil.isTrue(one != null,"该用户已经拥有Boss权限");
        UserPermissionConnect userPermissionConnect = new UserPermissionConnect();
        userPermissionConnect.setUserId(id)
                .setPermissionId(3);

        int insert = userPermissionConnectMapper.insert(userPermissionConnect);
        AssertUtil.isTrue(insert != 1,"添加boss权限失败");
        return RespBean.success("添加boss权限成功");
    }

    /**
      * @Author: 邓龙飞
      * @Date:
      * @Description: 回收admin权限
    */
    @Override
    public RespBean loseAdmin(Integer id) {
        UserPermissionConnect one = userPermissionConnectMapper.selectOne(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 1));
        AssertUtil.isTrue(one == null,"该用户本身没有Admin权限");
        int delete = userPermissionConnectMapper.delete(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 1));
        AssertUtil.isTrue(delete != 1,"回收admin权限失败");
        return RespBean.success("回收admin权限成功");
    }

    /**
      * @Author: 邓龙飞
      * @Date:
      * @Description: 赋予admin权限
    */
    @Override
    public RespBean addAdmin(Integer id) {
        UserPermissionConnect userPermissionConnect = new UserPermissionConnect();
        UserPermissionConnect one = userPermissionConnectMapper.selectOne(new QueryWrapper<UserPermissionConnect>().eq("user_id", id).eq("permission_id", 1));
        AssertUtil.isTrue(one != null,"该用户已经拥有Admin权限");
        userPermissionConnect.setUserId(id)
                .setPermissionId(1);
        int insert = userPermissionConnectMapper.insert(userPermissionConnect);
        AssertUtil.isTrue(insert != 1,"添加admin权限失败");
        return RespBean.success("添加admin权限成功");
    }
}
